package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.hotel.service.DeviceEventService;
import com.xdtech.hotel.vo.DeviceEventItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-05-05 22:31:28
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class DeviceEventController {
	@Autowired
	private DeviceEventService deviceEventService;
	@RequestMapping(value="/deviceEvent.do",params = "deviceEvent")
	public ModelAndView skipDeviceEvent() {
		return new ModelAndView("hotel/deviceEvent/deviceEvent_ftl");
	}
	
	@RequestMapping(value="/deviceEvent.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(DeviceEventItem item,Pagination pg) {
		Map<String, Object> results =  deviceEventService.loadPageDataByConditions(pg,item,"findDeviceEventByCondition");
		return results;
	}
	
	@RequestMapping(value="/deviceEvent.do",params = "editDeviceEvent")
	public ModelAndView editDeviceEvent(HttpServletRequest request,Long deviceEventId) {
		if (deviceEventId!=null) {
			request.setAttribute("deviceEventItem", deviceEventService.loadDeviceEventItem(deviceEventId));
		}
		return new ModelAndView("hotel/deviceEvent/editDeviceEvent_ftl");
	}
	
	@RequestMapping(value="/deviceEvent.do",params = "saveDeviceEvent")
	@ResponseBody
	public ResultMessage saveDeviceEvent(DeviceEventItem item) {
		ResultMessage r = new ResultMessage();
		if (deviceEventService.saveOrUpdateDeviceEvent(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/deviceEvent.do",params = "deleteDeviceEventItems")
	@ResponseBody
	public ResultMessage deleteDeviceEventItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> deviceEventIds = new ArrayList<Long>();
			for (String id : tempIds) {
				deviceEventIds.add(Long.valueOf(id));
			}
			deviceEventService.deleteDeviceEventInfo(deviceEventIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
